package net.kingborn.core.exception;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import net.kingborn.core.entry.Result;

/**
 * 统一异常处理类
 */
@ControllerAdvice
public class BaseExceptionHandler {

    @Value("${erp.devMode}")
    private boolean devMode;
	
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();

        if (e instanceof BizException) {
            BizException ex = (BizException) e;
            return new Result(false, ex.getErrorCode(), ex.getMessage());
        }

        return new Result(false, Result.CODE_FAILURE, devMode ? e.getMessage() : Result.ERROR_MESSAGE_UNKNOWN_EXCEPTION);
    }
}
